<?php

namespace App\Http\Controllers\V1\Admin;


use App\Models\V1\Message;
use App\Models\V1\Opinion;
use Illuminate\Http\Request;

class OpinionController extends BaseController
{

    /**
     * 信息列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $query = Opinion::query();
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'id');

        if ($request->filled('title')) {
            $query->where('title', 'like', "%" . $request->input('title', "") . "%");
        }
        if ($request->filled('mobile')) {
            $query->where('mobile', $request->input('mobile'));
        }


        if ($request->filled('community_id')) {
            $query->where('community_id', $request->input('community_id'));
        }

        if ($request->filled('opinion_class_id')) {
            $query->where('opinion_class_id', $request->input('opinion_class_id'));
        }

        if ($request->filled('state')) {
            $query->where('state', $request->input('state'));
        }



        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);

        return ajax_output(200, "成功", $data);
    }


    /**
     * 新增信息
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $model = new Opinion();
        $model->create([
            'title' => $request->input('title'),
            'message_class_id' => $request->input('message_class_id', ""),
            'message_class_name' => $request->input('message_class_name', ""),
            'describe' => $request->input('describe'),
            'thumb' => $request->input('thumb', ""),
            'contacts' => $request->input('contacts'),
            'mobile' => $request->input('mobile'),
            'remark' => $request->input('remark'),
            'check_state' => $request->input('check_state'),
            'user_id' => $request->input('user_id'),
            'username' => $request->input('username'),
            'community_id' => $request->input('community_id'),
            'community_name' => $request->input('community_name'),
            'state' => $request->input('state', 1)
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 信息详情
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $vo = Opinion::find($id);
        return ajax_output(200, "成功", $vo);
    }


    /**
     * 更新信息
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
//        return(111);
        $vo = Opinion::find($id);
        $vo->update([
            'title' => $request->input('title'),
            'message_class_id' => $request->input('message_class_id', ""),
            'message_class_name' => $request->input('message_class_name', ""),
            'describe' => $request->input('describe'),
            'thumb' => $request->input('thumb', ""),
            'contacts' => $request->input('contacts'),
            'mobile' => $request->input('mobile'),
            'remark' => $request->input('remark'),
            'check_state' => $request->input('check_state'),
            'user_id' => $request->input('user_id'),
            'username' => $request->input('username'),
            'community_id' => $request->input('community_id'),
            'community_name' => $request->input('community_name'),
            'state' => $request->input('state', 1)
        ]);
        return ajax_output(200, "成功");
    }


    /**
     * 删除信息
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $id = explode(',',$id);
        Opinion::destroy($id);
        return ajax_output(200, "成功");
    }
}
